package com.jimikongjian.service;

import com.jimikongjian.dao.ILike;
import com.jimikongjian.models.Like;
import com.jimikongjian.models.Message;
import com.jimikongjian.models.ResponseMessage;
import com.jimikongjian.service.base.IArticleService;
import com.jimikongjian.service.base.ILikeService;
import com.jimikongjian.service.base.IUserService;
import com.jimikongjian.service.message.ResponseMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by fuxiuyang on 17/3/14.
 */
@Service("likeService")
public class LikeService implements ILikeService {


    @Autowired
    @Qualifier("responseMessageService")
    private ResponseMessageService responseMessageService;


    @Autowired
    @Qualifier("iLikeImpl")
    private ILike iLikeImpl;


    @Autowired
    @Qualifier("userService")
    private IUserService userService;


    @Autowired
    @Qualifier("articleService")
    private IArticleService articleService;

    /**
     * 收藏某篇文章
     * @param like
     * @return
     */
    @Transactional
    public ResponseMessage likeArticle(Like like) {
        if (like.getUserId() > 0
                && like.getArticleId() > 0){
            if (userService.hasUser(like.getUserId())){
                if (articleService.hasArticle(like.getArticleId())){
                    if (!iLikeImpl.hasLike(like.getUserId(), like.getArticleId())){
                        iLikeImpl.insertLike(like);
                        articleService.updateAddArticleLikeCount(like.getArticleId());
                        return responseMessageService.makeMessage(Message.SUCCESS);
                    }
                    return responseMessageService.makeMessage(Message.SYSTEM_ERROR);
                }
                return responseMessageService.makeMessage(Message.NO_HAVE_ARTICLE);
            }
            return responseMessageService.makeMessage(Message.NO_HAVE_USER);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }


    /**
     * 通过用户的id，查询该用户收藏的所有的文章
     *
     * @param userId
     * @return
     */
    public ResponseMessage selectLikeByUserId(int userId) {
        if (userId > 0){
            if (userService.hasUser(userId)){
                List<Like> likeList = iLikeImpl.getLikeListByUserId(userId);
                return responseMessageService.makeMessage(likeList, Message.SUCCESS);
            }
            return responseMessageService.makeMessage(Message.NO_HAVE_USER);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 通过文章的id号查询收藏该文章的所有用户
     *
     * @param articleId
     * @return
     */
    public List<Like> selectLikeByArticleId(int articleId) {
        if (articleId > 0){
            if (articleService.hasArticle(articleId)){
                List<Like> likeList = iLikeImpl.getLikeListByArticleId(articleId);
                return likeList;
            }
        }
        return null;
    }

    /**
     * 删除某一用户收藏的某一篇文章
     *
     * @param like
     * @return
     */
    @Transactional
    public ResponseMessage deleteLike(Like like) {
        if (like.getUserId() > 0 && like.getArticleId() > 0){
            if (userService.hasUser(like.getUserId())){
                if (articleService.hasArticle(like.getArticleId())){
                    if (iLikeImpl.hasLike(like.getUserId(), like.getArticleId())){
                        iLikeImpl.deleteLike(like);
                        articleService.updateSubArticleLikeCount(like.getArticleId());
                        return responseMessageService.makeMessage(Message.SUCCESS);
                    }
                    return responseMessageService.makeMessage(Message.SYSTEM_ERROR);
                }
                return responseMessageService.makeMessage(Message.NO_HAVE_ARTICLE);
            }
            return responseMessageService.makeMessage(Message.NO_HAVE_USER);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 查询某一用户是否已收藏某篇文章
     * @param userId
     * @param articleId
     * @return
     */
    public boolean hadLike(int userId, int articleId) {
        if (articleId > 0){
            List<Like> likeList = selectLikeByArticleId(articleId);
            if (likeList.size() > 0){
                for (int i = 0; i < likeList.size(); i ++){
                    if (likeList.get(i).getUserId() == userId){
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * 查询某一用户是否已收藏某篇文章
     * @param userId
     * @param articleId
     * @return
     */
    public ResponseMessage selectLikeByUserIdAndArticleId(int userId, int articleId) {
        if (hadLike(userId, articleId)){
            return responseMessageService.makeMessage(Message.SUCCESS);
        }
        return responseMessageService.makeMessage(Message.NO_HAVE_LIKE);
    }
}
